#include "custommessagebox.h"

CustomMessageBox::CustomMessageBox()
{

}

CustomMessageBox::~CustomMessageBox()
{

}

/* 初始化提示框 */
void CustomMessageBox::initMsgBox(){
    this->_msgBox = new QMessageBox(this->_parent);
    /* 置顶 */
    this->_msgBox->setWindowFlags(this->_msgBox->windowFlags() | Qt::WindowStaysOnTopHint);

    /* 根据传入的窗口类型创建窗口对象 */
    switch(this->_msgType){
    /* 询问框 */
    case 1:
        this->_msgBox->setIcon(QMessageBox::Question);
        this->_msgBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        break;
    case 2:
        this->_msgBox->setIcon(QMessageBox::Warning);
        this->_msgBox->setStandardButtons(QMessageBox::Ok);
        break;
    case 3:
        this->_msgBox->setIcon(QMessageBox::Critical);
        this->_msgBox->setStandardButtons(QMessageBox::Ok);
        break;
    case 4:
        this->_msgBox->setIcon(QMessageBox::Information);
        this->_msgBox->setStandardButtons(QMessageBox::Ok);
        break;
    default:
        /* 无图标，无按钮，无边框 */
        this->_msgBox->setStandardButtons(QMessageBox::NoButton); // 指定没有任何按钮
        this->_msgBox->setAttribute(Qt::WA_ShowWithoutActivating); // 设置对话框不激活
        // 左上角关闭按钮不可用
        this->_msgBox->setWindowFlags(this->_msgBox->windowFlags() & ~(Qt::WindowContextHelpButtonHint | Qt::WindowCloseButtonHint));
        break;
    }

    /* 设置文字 */
    this->_msgBox->setText(this->_msgText);
    /* 设置标题 */
    this->_msgBox->setWindowTitle(this->_msgTitle);
    /* 设置中文字体 */
    this->changeButtonsFontType();

}

/* 提示框显示 */
int CustomMessageBox::show(QWidget *parent, CMsg msgType, QString msgTitle, QString msgText){
    this->_parent = parent;
    this->_msgType = msgType;
    this->_msgText = msgText;
    this->_msgTitle = msgTitle;

    this->initMsgBox();
    return this->_msgBox->exec();
}

/* 非事件循环式启动 */
void CustomMessageBox::normalShow(QWidget *parent, CMsg msgType, CMsg iconType, QString msgTitle, QString msgText){
    this->_parent = parent;
    this->_msgType = msgType;
    this->_msgText = msgText;
    this->_msgTitle = msgTitle;

    this->initMsgBox();
    this->setNormalShowIcon(msgType, iconType);
    this->_msgBox->show();
    /* 隐藏默认ok按钮 */
    if(msgType == 0){
        this->_msgBox->button(QMessageBox::Ok)->hide();
    }
    return;
}

/* 非事件循环式关闭 */
void CustomMessageBox::normalHide(){
    this->_msgBox->close();
    return;
}

/* 设置非事件循环式窗口图标 */
void CustomMessageBox::setNormalShowIcon(CMsg msgType, CMsg iconType){
    /* 如果为无按钮窗口，则设置图标 */
    if(msgType == 0){
        switch (iconType) {
        case 1:
            this->_msgBox->setIcon(QMessageBox::Question);
            break;
        case 2:
            this->_msgBox->setIcon(QMessageBox::Warning);
            break;
        case 3:
            this->_msgBox->setIcon(QMessageBox::Critical);
            break;
        case 4:
            this->_msgBox->setIcon(QMessageBox::Information);
            break;
        default:
            break;
        }
    }
}

/* 改变按钮文字类型 */
void CustomMessageBox::changeButtonsFontType(){
    this->_msgBox->setButtonText(QMessageBox::Ok, "确认");
    this->_msgBox->setButtonText(QMessageBox::Open, "打开");
    this->_msgBox->setButtonText(QMessageBox::Save, "保存");
    this->_msgBox->setButtonText(QMessageBox::Cancel, "取消");
    this->_msgBox->setButtonText(QMessageBox::Close, "关闭");
    this->_msgBox->setButtonText(QMessageBox::Discard, "Discard");
    this->_msgBox->setButtonText(QMessageBox::Apply, "应用");
    this->_msgBox->setButtonText(QMessageBox::Reset, "重置");
    this->_msgBox->setButtonText(QMessageBox::RestoreDefaults, "恢复默认");
    this->_msgBox->setButtonText(QMessageBox::Help, "帮助");
    this->_msgBox->setButtonText(QMessageBox::SaveAll, "保存全部");
    this->_msgBox->setButtonText(QMessageBox::Yes, "是");
    this->_msgBox->setButtonText(QMessageBox::YesToAll, "全是");
    this->_msgBox->setButtonText(QMessageBox::No, "否");
    this->_msgBox->setButtonText(QMessageBox::NoToAll, "全否");
    this->_msgBox->setButtonText(QMessageBox::Abort, "Abort");
    this->_msgBox->setButtonText(QMessageBox::Retry, "重试");
    this->_msgBox->setButtonText(QMessageBox::Ignore, "忽略");
}


